import os
import sys
import platform
import PyInstaller.__main__
import tkinterdnd2

# UPX配置
UPX_DIR = r"C:\Program Files\upx"  # 实际路径

# 入口文件
main_entry_point = "main.py"
switch_network_entry = "switch_network.py"

tkdnd_dir = os.path.join(os.path.dirname(tkinterdnd2.__file__), "tkdnd")
if not os.path.exists(tkdnd_dir):
    raise FileNotFoundError(
        f"未找到 tkdnd 目录，请确认 tkinterdnd2 是否安装正确。路径: {tkdnd_dir}"
    )

# 确定系统架构
is_64bit = platform.machine().endswith("64")
platform_dir = "win-x64" if is_64bit else "win-x86"
tkdnd_platform_path = os.path.join(tkdnd_dir, platform_dir)

if not os.path.exists(tkdnd_platform_path):
    raise FileNotFoundError(f"未找到 {platform_dir} 目录，路径: {tkdnd_platform_path}")

# 主程序打包参数
main_args = [
    main_entry_point,
    "--onefile",
    "--windowed",
    "--name=OTSlayer",
    "--clean",
    "--noconfirm",
    f"--add-data=app_icon.ico;.",
    f"--add-data={tkdnd_platform_path};tkdnd/{platform_dir}",  # 关键修改：添加特定平台的DLL
    f"--icon=app_icon.ico",
    f"--upx-dir={UPX_DIR}",
]

# 网络切换程序打包参数
switch_args = [
    switch_network_entry,
    "--onefile",
    "--name=switch_network",
    "--clean",
    "--noconfirm",
    "--uac-admin",
    "--windowed",
    "--runtime-tmpdir=.",
    f"--upx-dir={UPX_DIR}",
]

# 必要的隐藏导入
essential_hidden_imports = [
    "pystray._win32",
    "tkinterdnd2.tkinterdnd2",
    "PIL",
    "pywin32",
    "sqlite3",
]

# 添加隐藏导入
for imp in essential_hidden_imports:
    main_args.append(f"--hidden-import={imp}")
    switch_args.append(f"--hidden-import={imp}")

# 执行打包
print("打包网络切换程序...")
PyInstaller.__main__.run(switch_args)

print("打包主程序...")
PyInstaller.__main__.run(main_args)
